/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reyes.mysite.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import reyes.mysite.dao.GenericDAO;
import reyes.mysite.jdo.Blog;

/**
 *
 * @author reyesyang
 */
public class SaveBlogEditActionForm extends org.apache.struts.action.ActionForm {

    private String blogId;
    private String blogTitle;
    private String blogCategory;
    private String blogPrivacy;
    private String blogSummary;
    private String blogContent;

    public String getBlogId() {
        return blogId;
    }

    public void setBlogId(String blogId) {
        this.blogId= blogId;
    }

    public String getBlogTitle() {
        return blogTitle;
    }

    public void setBlogTitle(String blogTitle) {
        this.blogTitle = blogTitle;
    }

    public String getBlogCategory() {
        return blogCategory;
    }

    public void setBlogCategory(String blogCategory) {
        this.blogCategory = blogCategory;
    }

    public String getBlogPrivacy() {
        return this.blogPrivacy;
    }

    public void setBlogPrivacy(String blogPrivacy) {
        this.blogPrivacy = blogPrivacy;
    }

    public String getBlogSummary() {
        return this.blogSummary;
    }

    public void setBlogSummary(String blogSummary) {
        this.blogSummary = blogSummary;
    }

    public String getBlogContent() {
        return blogContent;
    }

    public void setBlogContent(String blogContent) {
        this.blogContent = blogContent;
    }

    public SaveBlogEditActionForm() {
        super();
    }
    
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        GenericDAO<Blog> blogDAO = new GenericDAO<Blog>();
        Blog blog = blogDAO.getById(Blog.class, Long.valueOf(blogId));
        request.setAttribute("blog", blog);

        ActionErrors errors = new ActionErrors();
        if (blogTitle == null || blogTitle.length() < 1) {
            errors.add("blogTitle", new ActionMessage("errors.nullOrEmpty"));
        }
        if(blogCategory.equals("not-chose")) {
            errors.add("blogCategory", new ActionMessage("errors.notChose"));
        }
        if(blogPrivacy.equals("not-chose")) {
            errors.add("blogPrivacy", new ActionMessage("errors.notChose"));
        }
        if(blogSummary == null || blogSummary.length() < 1) {
            errors.add("blogSummary", new ActionMessage("errors.nullOrEmpty"));
        }
        if(blogContent == null || blogContent.length() < 1) {
            errors.add("blogContent", new ActionMessage("errors.nullOrEmpty"));
        }
        return errors;
    }
}
